﻿using System;
using System.Collections.Generic;
using System.Text;
using LitJson;
using SynoManager.Communication;
using System.Collections.Specialized;

namespace SynoManager.AudioStation
{
    public class AudioStationClient : StationClientBase
    {
        private const int _audioStationServiceID = 5;
        private const string _playlistServiceUrl = @"/audio/playlist.cgi";
        private const string _playerServiceUrl = @"/audio/player.cgi";

        public AudioStationClient(SynoDevice device)
            : base(device, _audioStationServiceID, _playerServiceUrl)
        {
        }

        #region Player.cgi
        // Request URI: /audio/player.cgi?action=streaming&songpath=%2Fvolume2%2Fmusic%2FAlbums%2F....
        #endregion

        #region Playlist.cgi
        #region music
        public LibraryContents GetMusic(string category, string keyword, string sort, string direction, int start, int limit)
        {
            StringDictionary dict = new StringDictionary();

            dict.Add("action","music");
            dict.Add("category", category);
            dict.Add("keyword", keyword);
            dict.Add("sort", sort);
            dict.Add("dir", direction);
            dict.Add("start", start.ToString());
            dict.Add("limit", limit.ToString());
            
            string json = PostRequest(_playlistServiceUrl, dict);
            LibraryContents contents = JsonMapper.ToObject<LibraryContents>(json);
            return contents;
        }

        public class LibraryContents
        {
            public LibraryItem[] items;
            public int total;
        }

        public class LibraryItem
        {
            public string album;
            public string artist;
            public string duration;
            public string genre;
            public string id;
            public string index;
            public bool invalid;
            public string path;
            public string title;
        }
        #endregion
        #region list
        public PlaylistInfo[] GetPlaylists()
        {
            return GetPlaylists("", 0, 0);
        }

        internal PlaylistInfo[] GetPlaylists(string node, int start, int limit)
        {
            string json = PostRequest(_playlistServiceUrl, "action=list&node={0}&start={1}&limit={2}", node,
                                             start, limit);
            PlaylistInfo[] array = JsonMapper.ToObject<PlaylistInfo[]>(json);
            return array;
        }

        public class PlaylistInfo : ClientRequestResult
        {
            public string album;
            public string id;
            public bool leaf;
            public string qtip;
            public string text;
            public string type;
        }
        #endregion

        #region load
        public PlaylistContents GetPlaylistContents(string index, int start, int limit)
        {
            string json = PostRequest(_playlistServiceUrl, "action=load&index={0}&limit={1}&start={2}", index, limit,
                                      start);
            PlaylistContents contents = JsonMapper.ToObject<PlaylistContents>(json);
            return contents;
        }

        public class PlaylistContents
        {
            public PlaylistItem[] items;
            public int total;
        }

        public class PlaylistItem
        {
            public string album;
            public string artist;
            public string duration;
            public string genre;
            public int id;
            public int index;
            public bool invalid;
            public string path;
            public string title;
        }
        #endregion

        #region delete
        public PlaylistResult Delete(string playlistid)
        {
            return PostRequest<PlaylistResult>(_playlistServiceUrl, "action=delete&playlists={0}", playlistid);
        }

        public class PlaylistResult : ClientRequestResult
        {
            public PlaylistResultData data;
        }

        public class PlaylistResultData
        {
            public string index;
            public string title;
        }
        #endregion

        #region saveplsname
        public PlaylistResult Rename(string playlistid, string newname)
        {
            return PostRequest<PlaylistResult>(_playlistServiceUrl, "action=saveplsname&id={0}&playlist={1}", playlistid,
                                               newname);
        }
        #endregion

        #region removesongs
        public PlaylistResult RemoveSongs(string playlistid, int[] songids)
        {
            string[] ssongsids = new string[songids.Length];
            for (int i = 0; i < ssongsids.Length; i++)
                ssongsids[i] = songids[i].ToString();
            string id_list = String.Join(", ", ssongsids);
            return PostRequest<PlaylistResult>(_playlistServiceUrl, "action=removesongs&playlist={0}&id_list={1}",
                                               playlistid, id_list);
        }
        #endregion

        #region addsongs
        public PlaylistResult AddSongs(string playlistid, int[] songids)
        {
            string[] ssongsids = new string[songids.Length];
            for (int i = 0; i < ssongsids.Length; i++)
                ssongsids[i] = songids[i].ToString();
            string id_list = String.Join(", ", ssongsids);
            return PostRequest<PlaylistResult>(_playlistServiceUrl, "action=addsongs&playlist={0}&id_list={1}",
                                               playlistid, id_list);
        }
        #endregion

        #region add
        public PlaylistResult CreatePlaylist(string name, int[] songids)
        {
            StringDictionary dict = new StringDictionary();
            dict.Add("action", "add");
            dict.Add("name", name);
            if (songids.Length > 0)
            {
                string[] ssongsids = new string[songids.Length];
                for (int i = 0; i < ssongsids.Length; i++)
                    ssongsids[i] = songids[i].ToString();
                string id_list = String.Join(", ", ssongsids);
                dict.Add("id_list", id_list);
            }
            return PostRequest<PlaylistResult>(_playlistServiceUrl, dict);
        }
        #endregion
        #endregion
    }
}
